﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PlanesManagerWebAPI.Controllers
{
    public class FlightsController : ApiController
    {
        public IHttpActionResult GetFlightByID(string Id)
        {
            try
            {
                string token = Request.Headers.GetValues("token").FirstOrDefault();
                if (BusinessLoginLayer.Company_Details.ValidadeToken(token))
                {
                    CommonClasses.Flight flights = BusinessLoginLayer.Flights.GetFlightByID(Id);
                    return Ok(flights);
                }
                else
                {
                    System.Net.Http.Headers.AuthenticationHeaderValue v = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization");
                    return Unauthorized(v);
                }
            }
            catch (Exception)
            {
                return InternalServerError();
            }
        }

        [HttpGet]
        public IHttpActionResult QueryFlights(DateTime BeginDate, DateTime? EndDate, double? PriceLimit, string Origin, string Destination)
        {
            try
            {
                string token = Request.Headers.GetValues("token").FirstOrDefault();
                if (BusinessLoginLayer.Company_Details.ValidadeToken(token))
                {
                    CommonClasses.Flight[] flights = BusinessLoginLayer.Flights.GetFlightByQuery(BeginDate, EndDate, PriceLimit, Origin, Destination);
                    return Ok(flights);
                    //return Ok();
                }
                else
                {
                    System.Net.Http.Headers.AuthenticationHeaderValue v = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization");
                    return Unauthorized(v);
                }
            }
            catch (Exception)
            {
                return InternalServerError();
            }
        }
    }
}
